/*
 * rtc.c
 *
 *  Created on: 06-11-2012
 *      Author: Skeith
 */

#include "rtc.h"

void RtcInit(tU16 year, tU8 month, tU8 day, tU8 hour, tU8 minute, tU8 second)
{
	RTC_CCR  = 1 << 1;   //reset
	RTC_ILR  = 0;   //clear interrupt flags
	RTC_AMR  = 1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 7;   //nie porownuj z alarmami

	//32khz Crystal
	RTC_PREINT  = ((CRYSTAL_FREQUENCY * PLL_FACTOR) / VPBDIV_FACTOR) / 32768 - 1;
	RTC_PREFRAC = ((CRYSTAL_FREQUENCY * PLL_FACTOR) / VPBDIV_FACTOR) -
			  (((CRYSTAL_FREQUENCY * PLL_FACTOR) / VPBDIV_FACTOR) / 32768) * 32768;

	RTC_YEAR  = year;
	RTC_MONTH = month;
	RTC_DOM   = day;
	RTC_HOUR  = hour;
	RTC_MIN   = minute;
	RTC_SEC   = second;

	//if (use32khzCrystal == 0)
	RTC_CCR  = 0x01;   //start real-time clock, use core clock
	//else
	//RTC_CCR  = 0x00000011;   //start real-time clock, use 32kHz crystal
}

tU16 RtcGetTick()
{
	tU16 tick = RTC_CTC >> 1;
	return tick;
}
